package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.pojo.Specification;
import com.leyou.item.service.SpecificationService;
import com.leyou.item.service.impl.SpecificationServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author gsy
 * @date 2022/4/6
 * @apiNote
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 工具分类id查询分组
     */
    @GetMapping("groups/{cid}")
    private ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groups = this.specificationService.queryGroupsByCid(cid);
        if (CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid", required = false)Long gid,
            @RequestParam(value = "cid", required = false)Long cid,
            @RequestParam(value = "generic", required = false)Boolean generic,
            @RequestParam(value = "searching", required = false)Boolean searching
    ){

        List<SpecParam> params = this.specificationService.queryParams(gid, cid, generic, searching);

        if (CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }

    @PostMapping("group")
    @ResponseBody
    public ResponseEntity<List<SpecGroup>> saveGroups(@RequestBody SpecGroup specGroup){

        this.specificationService.saveSpecification(specGroup);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PutMapping("group")
    @ResponseBody
    public ResponseEntity<List<SpecGroup>> updateGroups(@RequestBody SpecGroup specGroup){
        this.specificationService.updateSpecification(specGroup);
        return ResponseEntity.status(HttpStatus.OK).build();
    }



    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroups(@PathVariable("id")Long id){
        SpecGroup specGroup = new SpecGroup();
        specGroup.setId(id);
        this.specificationService.deleteSpecification(specGroup);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
}

